
function h5Routes() {
  const context = require.context('../../router/h5', true, /^\.\/.+\.js$/)
  const routes = []
  context.keys().forEach(key => {
    routes.push(...context(key).default)
  })
  return routes
}
const defualtCacheRouter = () => {
  const routerList = h5Routes()
  const cacheRouter = routerList.filter(v => {
    return v.meta && v.meta.keepAlive === true
  })
  const routerNames = cacheRouter.map(v => v.name)
  return routerNames
}
const defautlNames = defualtCacheRouter()

const routerKeep = {
  state: {
    includedComponents: defautlNames, // 可缓存的组件名称
    excludedComponents: [] // 不可缓存的组件名称
  },
  mutations: {
    // 移除指定缓存
    removeInclude(state, name) {
      state.includedComponents.splice(state.includedComponents.indexOf(name), 1)
    },
    // 添加指定缓存
    addToInclude(state, name) {
      state.includedComponents.push(name)
    },
    // 清除所有的缓存
    clearInclude(state) {
      state.includedComponents = []
    },
    // 初始化指定缓存:路由名称
    initInclude(state, name) {
      state.includedComponents = name || defautlNames
    },
    removeExclude(state, name) {
      state.excludedComponents.splice(state.excludedComponents.indexOf(name), 1)
    },
    addToExclude(state, name) {
      state.excludedComponents.push(name)
    },
    clearExclude(state) {
      state.excludedComponents = []
    }
  }
}
export default { ...{ namespaced: true }, ...routerKeep }
